package com.entrogy.service.impl;

import com.entrogy.bean.ShiftEntity;
import com.entrogy.dao.AttendanceGroupMapper;
import com.entrogy.dao.ShiftMapper;
import com.entrogy.service.ShiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班次管理service接口实现类
 * @Author 许红松
 * @date 2019/5/9 0009 下午 2:51
 */
@Service("shiftService")
@Transactional
public class ShiftServiceImpl implements ShiftService {

    @Autowired
    private ShiftMapper shiftMapper;//班次管理dao接口
    @Autowired
    private AttendanceGroupMapper attendanceGroupMapper;//考勤组管理dao接口

    /**
     * 新增班次
     * @param shiftEntity
     * @return
     */
    @Override
    public int addShift(ShiftEntity shiftEntity) {
        return shiftMapper.addShift(shiftEntity);
    }

    /**
     * 修改班次信息
     * @param shiftEntity
     * @return
     */
    @Override
    public int UpdateShift(ShiftEntity shiftEntity) {
        return shiftMapper.UpdateShift(shiftEntity);
    }
    /**
     * 删除班次信息
     * @param shift_id
     * @return
     */
    public   int deleteShiftById(int school_code,int shift_id){
        Map<String,Object> map=new HashMap<>();
        map.put("school_code",school_code);
        List<Map<String,Object>> list=attendanceGroupMapper.getAttendanceGroupList(map);
        int Code=2;
        int m=list.size();
           if(m>0){
               int t=0;
               String  shift_Id=shift_id+"";
             for (int i=0;i<m;i++){
                      if (shift_Id.equals(list.get(i).get("shift1_Uptime"))){
                     t++;
                     break;
                 }else if(shift_Id.equals(list.get(i).get("shift1_Downtime"))){
                          t++;
                          break;
                 }
                  else if(shift_Id.equals(list.get(i).get("shift2_Uptime"))){
                          t++;
                          break;
                 }
                 else if(shift_Id.equals(list.get(i).get("shift1_Downtime"))){
                          t++;
                          break;
                 }
                 else if(shift_Id.equals(list.get(i).get("shift3_Uptime"))){
                          t++;
                          break;
                 }else if(shift_Id.equals(list.get(i).get("shift3_Downtime"))){
                          t++;
                          break;
                 }

             }
             if (t>0){
                 return  Code;
             }else{
                 return shiftMapper.deleteShiftById(shift_id);
             }

           }
            return shiftMapper.deleteShiftById(shift_id);
    };
    /**
     * 查询班次List信息
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> SelectShiftByList(Map<String, Object> map) {
        return shiftMapper.SelectShiftByList(map);
    }


    /**
     *   查询总数
     * @param map
     * @return
     */
    public int Total(Map<String,Object>  map){
        Integer code=shiftMapper.Total(map);
        if (code==0 || code==null){
            code=0;
        }
        return code;
    };


}
